package com.homwee.factory.pageData;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.homwee.factory.FactoryActivity;
import com.homwee.factory.FactoryEepromDialog;
import com.homwee.factory.FactoryModeService;
import com.homwee.factory.R;
import com.homwee.factory.WhiteBalanceAjustService;
import com.homwee.factory.util.VendorAppManager;
import com.homwee.listMenu.util.MenuItemData;
//zcj 2018-10-31 import android.os.SystemProperties;
//import com.changhong.dmt.system.miscservice.CHMiscServiceBinder;
//zcj 2018-10-30 import com.changhong.tvos.service.TvCommonManager;

public class PageData5 extends PageData{
	private Boolean flag;
	private Context context;
	List<MenuItemData> mSubList =new ArrayList<MenuItemData>();
	public PageData5(Context context,FactoryActivity mFactoryActivity){
		super(mFactoryActivity);
	}
	 String UartDebugStatus ="Please Click ▶";
	 MenuItemData ColorTempMode;
	 MenuItemData mRGain;
	 MenuItemData mGGain;
	 MenuItemData mBGain;
	 
	public List<MenuItemData> LoadPageData(){
		mListItemData.clear();
		mSubList.clear();
		String []powerOnModelArray = mFactoryActivity.getResources().getStringArray(R.array.factoryPowerOnModel);
		String powerOnModelStr = mFactoryActivity.getResources().getString(R.string.factory_power_on_model);
		MenuItemData powerOnModel =new MenuItemData(0,powerOnModelStr,0,2,0,powerOnModelArray,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		powerOnModel.mInitValue =factorySetting.getPowerMode();
		powerOnModel.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setPowerMode");
				factorySetting.setPowerMode(value);
			}
		});
		
		String []melancholyBoxSwitchArray = mFactoryActivity.getResources().getStringArray(R.array.factoryOpenOrClose);
		String melancholyBoxSwitchStr = mFactoryActivity.getResources().getString(R.string.factory_melancholy_Box_switch);
		MenuItemData melancholyBoxSwitch =new MenuItemData(0,melancholyBoxSwitchStr,0,1,0,melancholyBoxSwitchArray,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		melancholyBoxSwitch.mInitValue =factorySetting.getNonStandardFlag();
		melancholyBoxSwitch.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setNonStandardFlag");			
				factorySetting.setNonStandardFlag(value);
			}
		});
		
		//String[] mHdmiMode ={"关","类型1","类型2","类型3"};
		String[] mHdmiMode ={"Off","Type1","Type2","Type3"};
		
		MenuItemData mHDMINonstd =new MenuItemData(0,"HDMINonStandard",0,3,0,mHdmiMode,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mHDMINonstd.mInitValue =factorySetting.getHDMINonstdType();
		mHDMINonstd.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setHDMINonstdType");			
				factorySetting.setHDMINonstdType(value);
			}
		});
		
		String []leftRightMirrorImageArray = mFactoryActivity.getResources().getStringArray(R.array.factoryOpenOrClose);
		String leftRightMirrorImageStr = mFactoryActivity.getResources().getString(R.string.factory_left_right_mirror_image);
		MenuItemData leftRightMirrorImage =new MenuItemData(0,leftRightMirrorImageStr,0,1,0,leftRightMirrorImageArray,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		leftRightMirrorImage.mInitValue =factorySetting.getLRMirror();
		leftRightMirrorImage.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","pictureMode");
				factorySetting.setLRMirror(value);
			}
		});	
		
		String []upDownMirrorImageArray = mFactoryActivity.getResources().getStringArray(R.array.factoryOpenOrClose);
		String upDownMirrorImageStr = mFactoryActivity.getResources().getString(R.string.factory_up_down_mirror_image);
		MenuItemData upDownMirrorImage =new MenuItemData(0,upDownMirrorImageStr,0,1,0,upDownMirrorImageArray,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		upDownMirrorImage.mInitValue =factorySetting.getUDMirror();
		upDownMirrorImage.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","pictureMode");
				factorySetting.setUDMirror(value);
			}
		});	
		
//		MenuItemData debugMode =new MenuItemData(0,"logcat",0,1,0,upDownMirrorImageArray,1,EN_ItemType.EN_OPTIONVIEW);
//		debugMode.mInitValue =Settings.System.getInt(mContext.getContentResolver(), "ch-logcat", 1);
//		debugMode.setOnValueChange(new MenuItemData.onValueChange() {
//			
//			public void valueChange(int value) {
//				// TODO Auto-generated method stub
//				LogcatReader mLogcatReader =new LogcatReader(mContext);
//				
//				Settings.System.putInt(mContext.getContentResolver(), "ch-logcat", value);
//		        
//				if(value ==1){
//		        	mLogcatReader.removeFile();
//		        }else{
//		        	mLogcatReader.writeToFile();
//		        }  
//
//			}
//		});	
		
		String []designDebugArray = {"ERROR","WARN","INFO","DEBUG","VERBOSE"};
		String designDebugStr = mFactoryActivity.getResources().getString(R.string.factory_design_debug);
		MenuItemData designDebug =new MenuItemData(0,designDebugStr,0,4,0,designDebugArray,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		designDebug.mInitValue =factorySetting.getDebugFlag();
		designDebug.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setDebugFlag");			
				factorySetting.setDebugFlag(value);
			}
		});	
		
		String eepromInitializeStr = mFactoryActivity.getResources().getString(R.string.factory_eeprom_initialize);
		MenuItemData eepromInitialize =new MenuItemData(eepromInitializeStr);
		eepromInitialize.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				//mFactoryActivity.showMenu(false);
				Log.v("tv","resetEEP");
				 FactoryEepromDialog eepromDialog = new FactoryEepromDialog(mContext);
//				 mFactoryActivity.getMainLayout().setVisibility(View.INVISIBLE);
				 eepromDialog.show();
				 factorySetting.resetEEP();
				  
			}
		});	
		
		String cleanNetActiveStr = mFactoryActivity.getResources().getString(R.string.factory_clean_net_active);
		MenuItemData cleanNetActive =new MenuItemData(cleanNetActiveStr);
		cleanNetActive.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				Log.v("tv","onEnterRightKey");
				Toast toast = null;
				int ret = factorySetting.setClearNetActive(null);
				if (ret == 0)
				{
					toast = Toast.makeText(mContext, mContext.getString(R.string.factory_net_active_server_logout_ok_string), Toast.LENGTH_LONG);
//					Intent mIntent = new
//					Intent("com.changhong.tvos.shoppingpattern.broadcast"); 
//					mIntent.putExtra("staus", "close");      
//						    //�̳�ģʽ���Ļ� �ͷ��� ��open��
//					        //��֮����close��
//					context.sendBroadcast(mIntent); 
				} else if (ret == -1)
				{
					//Toast.makeText(context, "���������ע��ʧ�ܣ�ͨѶʧ�ܣ�", Toast.LENGTH_LONG).show();
					toast = Toast.makeText(mContext, mContext.getString(R.string.factory_net_active_server_logout_fail_communite_fail_string), Toast.LENGTH_LONG);
				} else if (ret == -2)
				{
					//Toast.makeText(context, "���������ע��ʧ�ܣ������������ӣ�", Toast.LENGTH_LONG).show();
					toast = Toast.makeText(mContext, mContext.getString(R.string.factory_net_active_server_logout_fail_check_netHW_string), Toast.LENGTH_LONG);
				} else
				{
					//Toast.makeText(context, "���������ע��ʧ�ܣ�", Toast.LENGTH_LONG).show();
					toast = Toast.makeText(mContext, mContext.getString(R.string.factory_net_active_server_logout_fail_general_string), Toast.LENGTH_LONG);
				}
//				toast.setGravity(mPaddingRight, 0, 0);
				toast.show();				
			}
		});	
		
		String cleanDataStr = mFactoryActivity.getResources().getString(R.string.factory_clean_data);
		MenuItemData cleanData =new MenuItemData(cleanDataStr);
		cleanData.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			public void onEnterRightKey() {		    
				
			    Toast toast = null;
		    	toast = Toast.makeText(mContext, mContext.getString(R.string.factory_clean_data_wait), Toast.LENGTH_SHORT);
		    	//toast.setGravity(mPaddingRight, 0, 0);
		    	toast.show();
				   Timer timer = new Timer();
				   TimerTask task = new TimerTask() {
						public void run() {

							factorySetting.cleanApp(false);
						}
				    }; 
				    timer.schedule(task, 200);
	}
		});	
		
		String againLandingStr = mFactoryActivity.getResources().getString(R.string.factory_again_landing);
		MenuItemData againLanding =new MenuItemData(againLandingStr);
		againLanding.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				//mFactoryActivity.showMenu(false);
				Log.v("tv","setAgainLand");
				factorySetting.setAgainLand();
			}
		});	
		
		MenuItemData RFFactoryMode =new MenuItemData("RFIrc FactoryMode");
		RFFactoryMode.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				//mFactoryActivity.showMenu(false);
//				CHMiscServiceBinder mChMiscServiceBinder =CHMiscServiceBinder.getInstance(mContext);
//				mChMiscServiceBinder.getUsbDevices().doRFDongleUIDReset(1);
			}
		});	
		
		MenuItemData vendorAppInstall =new MenuItemData("VendorAppInstall");
		vendorAppInstall.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, mContext.getString(R.string.factory_vendor_app_install_wait), Toast.LENGTH_SHORT).show();
				VendorAppManager mVendorAppManager =new VendorAppManager(mContext);
			}
		});	
		
		MenuItemData adbEnable =new MenuItemData("AdbEnable");
		adbEnable.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				//zcj 2018-10-31 SystemProperties.set("ctl.start", "adbd");				 
				Toast.makeText(mContext, " open adb", Toast.LENGTH_SHORT).show();
			}
		});			
		
		MenuItemData mAgingModel =new MenuItemData(0,"Aging Mode",0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mAgingModel.mInitValue =factorySetting.getAgingModel();
		mAgingModel.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setAgingModel");
				factorySetting.setAgingModel(value);
				Intent intentForBarServer = new Intent(mContext,FactoryModeService.class);
                mContext.startService(intentForBarServer);
			}
		});	
		MenuItemData mRecordAll = new MenuItemData(0, "Record All", 0, 1, 0,
				mFactoryActivity.getResources().getStringArray(
						R.array.factoryCloseOrOpen), 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mRecordAll.mInitValue = factorySetting.getPVRRecordAll();
		mRecordAll.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "getPVRRecordAll");
				factorySetting.setPVRRecordAll(value);
			}
		});
		
		MenuItemData mKeyUpgradeForce = new MenuItemData(0, "Key Upgrade Force", 0, 1, 0,
				mFactoryActivity.getResources().getStringArray(
						R.array.factoryCloseOrOpen), 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mKeyUpgradeForce.mInitValue = factorySetting.getKeyUpgradeForce();
		mKeyUpgradeForce.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "getPVRRecordAll");
				factorySetting.setKeyUpgradeForce(value);
			}
		});
		
		MenuItemData mSameService = new MenuItemData(0, "SameService", 0, 1, 0,
				mFactoryActivity.getResources().getStringArray(
						R.array.factorySameService), 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mSameService.mInitValue = factorySetting.getSameService()-1;
		Log.v("tv", "setSameService:"+(mSameService.mInitValue+1));
		mSameService.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "setSameService:"+(value+1));
				factorySetting.setSameService(value+1);
			}
		});
		
		
		MenuItemData mWhiteBalance =new MenuItemData(0,"WB Switch",0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mWhiteBalance.mInitValue =factorySetting.getWhiteBalance();
		mWhiteBalance.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setWhiteBalance");
				factorySetting.setWhiteBalance(value);
			}
		});	
		
		//uart on off
		MenuItemData mUartOnOff =new MenuItemData(0,"Uart",0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mUartOnOff.mInitValue =factorySetting.getUartOnOff();
		mUartOnOff.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setUartOnOff");
				factorySetting.setUartOnOff(value);
			}
		});
		//uart on off
		MenuItemData mNetworkAdb =new MenuItemData(0,"NetworkAdb",0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mNetworkAdb.mInitValue =factorySetting.getNetworkAdb();
		mNetworkAdb.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setUartOnOff");
				factorySetting.setNetworkAdb(value);
			}
		});


		//watch dog onoff
		MenuItemData mWatchDogMode =new MenuItemData(0,"Watch Dog",0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mWatchDogMode.mInitValue =factorySetting.getWatchDogMode();
		mWatchDogMode.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setWatchDogMode");
				factorySetting.setWatchDogMode(value);
			}
		});	
		
		//WOL
		MenuItemData mWifiOnOff =new MenuItemData(0,"WOL",0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mWifiOnOff.mInitValue =factorySetting.getWOWOnOff();
		mWifiOnOff.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","setWIFIOnOff");
				factorySetting.setWOWOnOff(value);
			}
		});	
		
		//WOW
				MenuItemData mWOWOnOff =new MenuItemData(0,"WOW",0,1,0,mFactoryActivity.getResources().getStringArray(R.array.factoryCloseOrOpen),1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
				mWOWOnOff.mInitValue =factorySetting.getWIFIOnOff();
				mWOWOnOff.setOnValueChange(new MenuItemData.onValueChange() {
					
					public void valueChange(int value) {
						// TODO Auto-generated method stub
						Log.v("tv","setWOWOnOff");
						factorySetting.setWIFIOnOff(value);
					}
				});	
		
				// factorySetting.getHDCPKey2x_CheckSum()?"OK":"Error";
				 final MenuItemData MstToolUartDebug  = new MenuItemData(UartDebugStatus, "Uart Debug",UartDebugStatus, MenuItemData.EN_ItemType.EN_OPTIONTEXTVIEW);
				 MstToolUartDebug.setOnValueChange(new MenuItemData.onValueChange() {
						
						public void valueChange(int value) {
							// TODO Auto-generated method stub
							Log.v("tv","MstToolUartDebug-->EnableUartDebug");
							if(factorySetting.EnableUartDebug()){
								UartDebugStatus="OK";															
							}else{
								UartDebugStatus="failed";								
							}
							MstToolUartDebug.updateValue(UartDebugStatus);	
								
						}
					});	
		
				 
		// white balance adjust		 
		MenuItemData mWhiteBalanceAdjust =new MenuItemData("WB Adjust");
		
		mWhiteBalanceAdjust.setOnEnterKey(new MenuItemData.onEnterRightKey() {
			
			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				//mFactoryActivity.showMenu(false);
				Log.v("tv","resetEEP");
				Intent TVtoolServiceIntent = new Intent(mFactoryActivity,WhiteBalanceAjustService.class);
				TVtoolServiceIntent.setAction("com.homwee.factory.WhiteBalanceAjustService");
				mFactoryActivity.startService(TVtoolServiceIntent);
				mFactoryActivity.finish();
				  
			}
		});
		
		String [] srcArray =  { "HDMI", "YPBPR", "VGA", "CVBS", "SVIDEO", "SCART", "ATV", "DTV" };
		
		MenuItemData WB_Src =new MenuItemData(0,"Source",0,srcArray.length-1,1,srcArray,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		WB_Src.mInitValue =factorySetting.getSourceIndex();
		WB_Src.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","WB Source");			
				factorySetting.setSourceIndex(value);
				
				factorySetting.setColorTempMode(1);//standard				
				ColorTempMode.updateValue(1);
				
				mRGain.updateValue(factorySetting.getRGain());
				mGGain.updateValue(factorySetting.getGGain());
				mBGain.updateValue(factorySetting.getBGain());
				
				
			}
		});	
		
		String []ColorTempArray = {"Warm","Standard","Cool"};
		ColorTempMode =new MenuItemData(0,"Color Temperature",0,2,1,ColorTempArray,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		ColorTempMode.mInitValue =factorySetting.getColorTempMode();
		ColorTempMode.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","ColorTempMode"+value);			
				factorySetting.setColorTempMode(value);
				
				mRGain.updateValue(factorySetting.getRGain());
				mGGain.updateValue(factorySetting.getGGain());
				mBGain.updateValue(factorySetting.getBGain());
				
			}
		});	
		
		 mRGain =new MenuItemData(0,"R Gain",0,100,50,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mRGain.mInitValue =factorySetting.getRGain();
		mRGain.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","mRGain");			
				//factorySetting.setRGain(value);
			}
		});	
			
		 mGGain =new MenuItemData(0,"G Gain",0,100,50,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mGGain.mInitValue =factorySetting.getGGain();
		mRGain.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","mGGain");			
				factorySetting.setGGain(value);
			}
		});	
		
		 mBGain =new MenuItemData(0,"B Gain",0,100,50,1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mBGain.mInitValue =factorySetting.getBGain();
		mRGain.setOnValueChange(new MenuItemData.onValueChange() {
			
			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv","mBGain");			
				factorySetting.setBGain(value);
			}
		});	
				
		
		
		mListItemData.add(powerOnModel);
//		mListItemData.add(melancholyBoxSwitch);
		
		mListItemData.add(mAgingModel);
		//mListItemData.add(mRecordAll);
		mListItemData.add(mKeyUpgradeForce);
		
		mListItemData.add(mWhiteBalanceAdjust);		
		mListItemData.add(mWhiteBalance);
		
		//mListItemData.add(leftRightMirrorImage);
		//mListItemData.add(upDownMirrorImage);
//		mListItemData.add(debugMode);
		//mListItemData.add(designDebug);
		
		
		//mListItemData.add(eepromInitialize);
		//mListItemData.add(MstToolUartDebug);
		
		//wifi
		mListItemData.add(mWifiOnOff);
		mListItemData.add(mWOWOnOff);
		
		mListItemData.add(mUartOnOff);
		mListItemData.add(mNetworkAdb);
		mListItemData.add(mWatchDogMode);
		
//		mListItemData.add(cleanNetActive);
		mListItemData.add(cleanData);
//		mListItemData.add(vendorAppInstall);
//		mListItemData.add(RFFactoryMode);
//		mListItemData.add(adbEnable);
		//mListItemData.add(againLanding);
		//mListItemData.add(mHDMINonstd);
		
		
		//WB adjust  sub
		/*mSubList.add(WB_Src);
		mSubList.add(ColorTempMode);
		mSubList.add(mRGain);
		mSubList.add(mGGain);
		mSubList.add(mBGain);*/
		
		
		/*WB_Src.mParentGroup =mListItemData;
		WB_Src.mParent = mWhiteBalanceAdjust;
		mWhiteBalanceAdjust.mSubChildGroup =mSubList;*/
		
		return mListItemData;
	}
}
